package com.hl.payServer.task;


import com.hl.payServer.model.PayTradeInfo;
import com.hl.payServer.model.enums.TradeState;
import com.hl.payServer.model.enums.TradeType;
import com.hl.payServer.service.PayTradeInfoService;
import com.hl.payServer.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by Administrator on 2018/10/4 0004.
 */
@Component("queryTask")
public class QueryTask {
    private final Logger log = LoggerFactory.getLogger(QueryTask.class);

    @Autowired
    protected PayTradeInfoService payTradeInfoService;

    /**
     * 3秒执行一次,查询微信刷卡支付交易状态不明确的三种订单在去微信那确认
     * 直接调用查询方法，查询方法的逻辑：先查库， 如果库里的状态不是支付中，直接返回，如果是支付中，就去第三方查询，结果会更DAO本地
     */
    @Scheduled(fixedRate = 1000*5)
    void queryMicropay() {
        log.info("查询刷卡支付订单状态定时器开始");
        List<String> list = new ArrayList<String>(3);
        list.add(TradeState.USERPAYING.type());
//        list.add(TradeState.BANKERROR.type());
//        list.add(TradeState.SYSTEMERROR.type());
        HashMap<String, Object> params = new HashMap<>();
        params.put("statusList", list);// 必须要放在list里，因为共用
        List<String> tradeTypeList = new ArrayList<String>(1);
        tradeTypeList.add(TradeType.MICROPAY.type());
        params.put("tradeTypeList", tradeTypeList);
//        params.put("createTimeStart", DateUtil.getTimeByHour(-2));

        List<PayTradeInfo> payTradeInfos = payTradeInfoService.queryListSelfUse(params);
        if (payTradeInfos != null) {
            for (PayTradeInfo payTradeInfo : payTradeInfos) {
                String status = payTradeInfo.getStatus();
                HashMap<String, Object> queryParam = new HashMap<>();
                queryParam.put("platformPayNo", payTradeInfo.getTradeNo());
                try {
                    PayTradeInfo queryResponse = payTradeInfoService.queryThird(queryParam, payTradeInfo);
                    long s = DateUtil.getTimeDifferenceSencond(payTradeInfo.getCreateTime(), new Date());
//                    if (Math.abs(s) > 30) {
//                        //超过30秒后做后续处理    如果超过了,做撤消订单
//                        payTradeInfoService.cancelOrderByOutTradeNo(payTradeInfo);
//                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 30秒执行一次,查询超过半小时的正在支付中的订单,查出结果后,直接进行状态修改
     */
    @Scheduled(fixedRate = 1000*30)
    void queryUserPaying() {
        log.info("查询超时支付订单定时器开始");
        List<String> list = new ArrayList<String>(3);
        list.add(TradeState.USERPAYING.type());
        HashMap<String, Object> params = new HashMap<>();
        params.put("statusList", list);// 必须要放在list里，因为共用
        params.put("createTimeEnd", DateUtil.getSomeSecondsAgo("",60*30));
        List<PayTradeInfo> payTradeInfos = payTradeInfoService.queryListSelfUse(params);
        if (payTradeInfos != null) {
            for (PayTradeInfo payTradeInfo : payTradeInfos) {
                String status = payTradeInfo.getStatus();
                HashMap<String, Object> queryParam = new HashMap<>();
                queryParam.put("platformPayNo", payTradeInfo.getTradeNo());
                try {
                    payTradeInfoService.queryThird(queryParam, payTradeInfo);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

}
